package pbbltobpel.model.steps;

import org.w3c.dom.Document;
import org.w3c.dom.Element;

import pbbltobpel.model.IXmlWriter;

public class Copy implements IXmlWriter {
	private CopyParameter from;
	private CopyParameter to;
	
	public CopyParameter getTo() {
		return to;
	}
	public void setTo(CopyParameter to) {
		this.to = to;
	}
	public CopyParameter getFrom() {
		return from;
	}
	public void setFrom(CopyParameter from) {
		this.from = from;
	}
	
	@Override
	public Element getXmlElement(Document doc) {
		Element element = doc.createElement("bpel:copy");
		Element fromElement = doc.createElement("bpel:from");
		addData(fromElement, from,doc);
		Element toElement = doc.createElement("bpel:to");
		addData(toElement, to, doc);
		element.appendChild(fromElement);
		element.appendChild(toElement);
		return element;
	}
	private void addData(Element element, CopyParameter par, Document doc) {
		if(par.getPart() != null)
			element.setAttribute("part", par.getPart());
		if(par.getVariable() != null)
			element.setAttribute("variable", par.getVariable().getName());
		if(LiteralParameter.class.isInstance(par)){
			Element e = doc.createElement("bpel:literal");
			e.setTextContent(((LiteralParameter)par).getValue());
			element.appendChild(e);
		}
		else if(QueryParameter.class.isInstance(par)){
			Element e = doc.createElement("bpel:query");
			QueryParameter qp = (QueryParameter)par;
			e.setAttribute("queryLanguage", qp.getQueryLanguage());
			e.setTextContent(qp.getQuery());
			element.appendChild(e);
		}
		
	}
}
